package com.xinchen.aiowaste.vm

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.maple.model.data.AioUserInfo
import com.maple.model.entity.aio.LabelInfoEntity
import com.maple.model.entity.aio.LabelListEntity
import com.maple.model.entity.aio.WarehousEntity
import com.maple.model.entity.aio.WarehouseListEntity
import com.maple.utils.manager.SingleLiveEvent
import com.maple.utils.store.AioDBHelper
import com.xinchen.aiowaste.base.BaseViewModel
import com.xinchen.aiowaste.model.repository.CommonRepository
import java.util.WeakHashMap

class LabelListViewModel: BaseViewModel(){


    private val repository by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { CommonRepository() }

    val userInfo: AioUserInfo? by lazy {
        AioDBHelper.getUser()
    }

    val entId: String by lazy {
        userInfo?.entId?:""
    }

    val userId: String by lazy {
        userInfo?.userId?:""
    }

    // 下拉刷新事件
    val refreshEvent: SingleLiveEvent<Any> = SingleLiveEvent()
    // 加载更多事件
    val loadMoreEvent: SingleLiveEvent<Any> = SingleLiveEvent()


    val labelList: MutableLiveData<List<LabelListEntity.DataList>> = MutableLiveData()
    val refreshLabelList: MutableLiveData<List<LabelListEntity.DataList>> = MutableLiveData()
    val loadMoreLabelList: MutableLiveData<List<LabelListEntity.DataList>> = MutableLiveData()

    fun getLabelList(isPrint: String?,qrCode: String?,createdAt: String?,createdAt1: String?) {
        resetPage()

        val params: WeakHashMap<String, Any> = WeakHashMap()
//        // isPrint "" 不限, "1" 已打印 , "0" 未打印
        params.put("isPrint", isPrint)
        if(!TextUtils.isEmpty(qrCode)) {
            params.put("qrcode", qrCode)
        }
        if(!TextUtils.isEmpty(createdAt)) {
            params.put("createdAt", createdAt)
        }
        if(!TextUtils.isEmpty(createdAt1)) {
            params.put("createdAt1", createdAt1)
        }
        params.put("start", getPageNum().toString())
        params.put("entId", entId)
        launchOnlyResult(
            block = {
                repository.labelList(params)
            }, success = {
                val entity = GsonUtils.fromJson(GsonUtils.toJson(it), LabelListEntity::class.java)
                entity.data?.let { data ->
                    setNoMoreData(data.count)
                    if(!data.list.isNullOrEmpty()) {
                        labelList.postValue(data.list)
                        nextPage()
                    } else {
                        defUI.onEmpty()
                    }
                }?:let {
                    defUI.onEmpty()
                }
            }, error = {
                defUI.onEmpty()
            })
    }

    fun refreshLabelList(isPrint: String?,qrCode: String?,createdAt: String?,createdAt1: String?) {
        resetPage()

        val params: WeakHashMap<String, Any> = WeakHashMap()
        params.put("isPrint", isPrint)
        if(!TextUtils.isEmpty(qrCode)) {
            params.put("qrcode", qrCode)
        }
        if(!TextUtils.isEmpty(createdAt)) {
            params.put("createdAt", createdAt)
        }
        if(!TextUtils.isEmpty(createdAt1)) {
            params.put("createdAt1", createdAt1)
        }
        params.put("start", getPageNum().toString())
        params.put("entId", entId)
        launchOnlyResult(
            block = {
                repository.labelList(params)
            }, success = {
                val entity = GsonUtils.fromJson(GsonUtils.toJson(it), LabelListEntity::class.java)
                entity.data?.let { data ->
                    setNoMoreData(data.count)
                    if(!data.list.isNullOrEmpty()) {
                        refreshLabelList.postValue(data.list)
                        nextPage()
                    }
                }
            }, complete = {
                refreshEvent.call()
            })
    }

    fun loadMoreLabelList(isPrint: String?,qrCode: String?,createdAt: String?,createdAt1: String?) {
        val params: WeakHashMap<String, Any> = WeakHashMap()
        params.put("isPrint", isPrint)
        if(!TextUtils.isEmpty(qrCode)) {
            params.put("qrcode", qrCode)
        }
        if(!TextUtils.isEmpty(createdAt)) {
            params.put("createdAt", createdAt)
        }
        if(!TextUtils.isEmpty(createdAt1)) {
            params.put("createdAt1", createdAt1)
        }
        params.put("start", getPageNum().toString())
        params.put("entId", entId)
        launchOnlyResult(
            block = {
                repository.labelList(params)
            }, success = {
                val entity = GsonUtils.fromJson(GsonUtils.toJson(it), LabelListEntity::class.java)
                entity.data?.let { data ->
                    setNoMoreData(data.count)
                    if(!data.list.isNullOrEmpty()) {
                        loadMoreLabelList.postValue(data.list)
                        nextPage()
                    }
                }
            }, complete = {
                loadMoreEvent.call()
            })
    }
}